
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
/**
 * @author liyiruo
 * @Description
 * @Date 2024/8/15 上午11:19
 */
public class DerbyExample {
    public static void main(String[] args) {
        try {
            // 1. 加载 Derby 驱动
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

            // 2. 连接到 Derby 数据库
            Connection connection = DriverManager.getConnection("jdbc:derby:memory:testdb;create=true");

            // 3. 创建 Statement 对象
            Statement statement = connection.createStatement();

            // 4. 执行 SQL 查询
            statement.executeUpdate("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))");
            statement.executeUpdate("INSERT INTO users (id, name) VALUES (1, 'Alice')");

            // 5. 查询数据
            ResultSet rs = statement.executeQuery("SELECT * FROM users");
            while (rs.next()) {
                System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
            }

            // 6. 关闭资源
            rs.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

